package com.zlfund.cat.zjgsmk.ui.home;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.api.ZjgsmkService;
import com.zlfund.cat.zjgsmk.bean.HomeBean;
import com.zlfund.cat.zjgsmk.constant.EventBusConstant;
import com.zlfund.cat.zjgsmk.constant.MessageEvent;
import com.zlfund.cat.zjgsmk.net.INetCallback;
import com.zlfund.cat.zjgsmk.net.RetrofitFiledRequest;
import com.zlfund.cat.zjgsmk.net.RetrofitNoParamRequest;
import com.zlfund.cat.zjgsmk.net.RxNetErrorEmptyImp;
import com.zlfund.cat.zjgsmk.ui.base.BaseActivity;
import com.zlfund.cat.zjgsmk.ui.home.smk_activity.MainActivityFragment;
import com.zlfund.cat.zjgsmk.ui.home.smk_serve.MainServeFragment;
import com.zlfund.cat.zjgsmk.ui.home.smk_square.MainSquareFragment;
import com.zlfund.cat.zjgsmk.ui.home.smk_user.MainUserFragment;
import com.zlfund.cat.zjgsmk.ui.user.UserManager;
import com.zlfund.cat.zjgsmk.widget.RadioButton;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.fm_container)
    FrameLayout mFmContainer;
    @BindView(R.id.rb_main_serve)
    RadioButton mRbMainServe;
    @BindView(R.id.rb_main_square)
    RadioButton mRbMainSquare;
    @BindView(R.id.rb_main_activity)
    RadioButton mRbMainActivity;
    @BindView(R.id.rb_main_user)
    RadioButton mRbMainUser;
    @BindView(R.id.rg_tab)
    RadioGroup mRgTab;

    private Fragment mServeFragment;
    private Fragment mSquareFragment;
    private Fragment mActivityFragment;
    private Fragment mUserFragment;
    private int mCheckedIndex;
    private FragmentManager mFm;
    private ArrayList<Fragment> mFragments;
    private HomeBean mHomeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null){
            mCheckedIndex = savedInstanceState.getInt("checkedIndex");
            mHomeBean = (HomeBean) savedInstanceState.getSerializable("homeBean");
        }
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("homeBean",mHomeBean);
        outState.putInt("checkedIndex",mCheckedIndex);
    }

    @Override
    protected void setListener() {
        mRbMainActivity.setOnCheckedChangeListener(this);
        mRbMainServe.setOnCheckedChangeListener(this);
        mRbMainSquare.setOnCheckedChangeListener(this);
        mRbMainUser.setOnCheckedChangeListener(this);
        ((android.widget.RadioButton)mRgTab.getChildAt(mCheckedIndex)).setChecked(true);
    }

    @Override
    protected void initData() {
        if (mHomeBean == null) {
            requestDataFromUrl();
        }
        mFragments = new ArrayList<>();
        mFm = getSupportFragmentManager();
        mServeFragment = Fragment.instantiate(mContext,MainServeFragment.class.getCanonicalName());
        mSquareFragment = Fragment.instantiate(mContext, MainSquareFragment.class.getCanonicalName());
        mActivityFragment = Fragment.instantiate(mContext, MainActivityFragment.class.getCanonicalName());
        mUserFragment = Fragment.instantiate(mContext, MainUserFragment.class.getCanonicalName());
        mFragments.add(mServeFragment);
        mFragments.add(mSquareFragment);
        mFragments.add(mActivityFragment);
        mFragments.add(mUserFragment);
        setFragmentData();
    }

    private void setFragmentData(){
        if (mHomeBean != null){
            ((MainServeFragment)mServeFragment).setServeBannerBeans(mHomeBean.getServiceBar());
            ((MainServeFragment)mServeFragment).setServeInfoBeans(mHomeBean.getServiceInfo());
            ((MainSquareFragment)mSquareFragment).setList(mHomeBean.getTopimg());
            ((MainSquareFragment)mSquareFragment).setChannelBeans(mHomeBean.getSuggest(),mHomeBean.getFocus());
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event){
        switch (event.getCode()){
            case EventBusConstant.MAINACTIVITYREFRESHDATA:
                requestDataFromUrl();
                break;
        }
    }

    private void requestDataFromUrl() {
        new RetrofitFiledRequest<HomeBean>(UserManager.getAuth())
                .build(ZjgsmkService.ChannelHomeService.class)
                .enqueueWithRx("getChannelHomeInfo", new INetCallback<HomeBean>() {
                    @Override
                    public void response(HomeBean homeBean) {
                        mHomeBean = homeBean;
                        setFragmentData();
                    }

                    @Override
                    public void error(String str) {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            int index = mRgTab.indexOfChild(buttonView);
            FragmentTransaction transaction = mFm.beginTransaction();
            Fragment fragment = mFragments.get(index);
            if (!fragment.isAdded()){
                transaction.add(R.id.fm_container,fragment);
            }
            transaction.show(fragment);
            if (mCheckedIndex != index){
                Fragment preFragment = mFragments.get(mCheckedIndex);
                transaction.hide(preFragment);
                mCheckedIndex = index;
            }
            transaction.commit();
        }
    }

}
